package com.example.customviewring.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.example.customviewring.R;

/**
 * Created by AD on 2017/4/14.
 */

public class CustomViewRing extends View {
    /**
     * 第一圈的颜色
     */
    private int mFirstColor;

    /**
     * 第二圈的颜色
     */
    private int mSecondColor;
    /**
     * 圆的宽度
     */
    private int mCircleWidth;
    /**
     * 画笔
     */
    private Paint mPaint;
    /**
     * 当前进度
     */
    private int mProgress;
    /**
     * 速度
     */
    private int mSpeed;
    /**
     * 是否应该开始下一个
     */
    private boolean isNext = false;
    public CustomViewRing(Context context) {
        this(context,null);
    }

    public CustomViewRing(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    /**
     * 必要的初始化,获得一些自定义的属性
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public CustomViewRing(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomViewRing,defStyleAttr,0);
        int n = array.getIndexCount();
        for(int i=0; i < n ; i++){
            int attr = array.getIndex(i);
            switch (attr){
                case R.styleable.CustomViewRing_firstColor:
                    mFirstColor = array.getColor(attr,Color.GREEN);
                    break;
                case R.styleable.CustomViewRing_secondColor:
                    mSecondColor = array.getColor(attr, Color.RED);
                    break;
                case R.styleable.CustomViewRing_circleWidth:
                    mCircleWidth = array.getDimensionPixelSize(attr,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,20,getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CustomViewRing_speed:
                    mSpeed = array.getInt(attr,20);//默认20
                    break;
            }
        }
        array.recycle();
        mPaint = new Paint();
        //绘图线程
        new Thread()
        {
            public void run()
            {
                while (true)
                {
                    mProgress++;
                    if (mProgress == 360)
                    {
                        mProgress = 0;
                        if (!isNext)
                            isNext = true;
                        else
                            isNext = false;
                    }
                    postInvalidate();
                    try
                    {
                        Thread.sleep(mSpeed);
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            };
        }.start();

       /* new Thread() {
            public void run() {
                while (true) {
                    mProgress++;
                    if (mProgress == 360) {
                        mProgress = 0;
                        int tempColor = mFirstColor;
                        mFirstColor = mSecondColor;
                        mSecondColor = tempColor;
                    }
                    postInvalidate();
                    try {
                        Thread.sleep(mSpeed);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();*/
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int centre = getWidth() / 2;//获取圆心的坐标
        int radius = centre - mCircleWidth / 2; // 半径
        mPaint.setStrokeWidth(mCircleWidth);//设置圆环的宽度
        mPaint.setAntiAlias(true);//消除锯齿
        mPaint.setStyle(Paint.Style.STROKE);//设置空心
        RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius);//用于定义圆弧的形状和大小的界限
        if(!isNext){//第一圈的圆完整，第二圈的圆跑
            mPaint.setColor(mFirstColor);//设置圈环的颜色
            canvas.drawCircle(centre,centre,radius,mPaint);//画出圆环
            mPaint.setColor(mSecondColor);//设置圆环的颜色
            canvas.drawArc(oval, -90, mProgress, false, mPaint);//根据进度画圆弧
        }else {
            mPaint.setColor(mSecondColor);//设置圆环的颜色
            canvas.drawCircle(centre, centre, radius, mPaint);
            mPaint.setColor(mFirstColor);//设置圆环的颜色
            canvas.drawArc(oval, -90, mProgress, false, mPaint);//根据进度画圆弧
        }
    }

}
